import Vue from "vue";
import Vuex from "vuex";
import quinn from "@/utils/quinn";
import { STORE_LEDGER_COLUMN } from "@/assets/js/define.js";

Vue.use(Vuex);

const state = {
  areaLedgerType: [],
  storeLedgerTypeStore: [],
  storeLedgerType: [],
  userData: {},
  cacheRouter: [],
  token: "",
  roleMap: {},
  directoryMap: {},
  accountMap: {},
  leaderMap: {},
  areaMap: {},
  areaList: [
    {
        "area": 54,
        "areaName": "武汉"
    },
    {
        "area": 43,
        "areaName": "宁乡壹"
    },
    {
        "area": 44,
        "areaName": "广东"
    },
    {
        "area": 45,
        "areaName": "广西"
    },
    {
        "area": 52,
        "areaName": "贵州"
    },
    {
        "area": 53,
        "areaName": "云南"
    },
    {
        "area": 46,
        "areaName": "宁乡贰"
    }
],
  aftersaleMap: {},
  authority: [],
};

const getters = {
  aaa({ authority }) {
    console.log(authority);
    return [1];
  },
};

const mutations = {
  modify(state, map = {}) {
    for (const key in map) {
      if (Object.hasOwnProperty.call(map, key)) {
        const value = map[key];
        if (Object.hasOwnProperty.call(state, key)) state[key] = value;
      }
    }
  },
};

const actions = {
  refreshStoreLedgerStore(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        window.$api
          // .newStoreLedgerStores()
          .storeLedgerStoreLedgerList({pageNumber:1,pageSize:20})
          .then((response) => {
            let res = response.data
            res = (res || []).map((e) => ({
              label: e.storeName,
              value: e.orderId,
            }));
            commit("modify", {
              storeLedgerTypeStore: res || [],
            });
            resolve(res);
          })
          .catch(reject);
      } catch (error) {
        console.log("error", error);
        reject(error);
      }
    });
  },
  refreshStoreLedgerType(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        window.$api
          .newStoreLedgerTypes()
          .then((res) => {
            commit("modify", {
              storeLedgerType: res || STORE_LEDGER_COLUMN,
            });
            resolve(res);
          })
          .catch(reject);
      } catch (error) {
        console.log("error", error);
        reject(error);
      }
    });
  },
  refreshAreaLedgerType(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        window.$api
          .areaLedgerFyTypeList()
          .then((res) => {
            commit("modify", {
              areaLedgerType: res,
            });
            resolve(res);
          })
          .catch(reject);
      } catch (error) {
        console.log("error", error);
        reject(error);
      }
    });
  },
  refreshAuthority(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        const { id } = quinn.getStorage("userData") || {};
        window.$api
          .getAccountInfo({ id })
          .then((res) => {
            const { authority = [] } = res || {};
            commit("modify", {
              authority,
            });
            resolve(res);
          })
          .catch(reject);
      } catch (error) {
        console.log("error", error);
        reject(error);
      }
    });
  },
  refreshArea(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        window.$api
          .areaLedgerAreaList()
          .then((res) => {
            const map = {};
            (res || []).forEach(({ areaName, area}) => {
              map[area] = areaName;
            });

            commit("modify", {
              areaList: res,
              areaMap: map,
            });
            resolve(res);
          })
          .catch(reject);
      } catch (error) {
        console.log("error", error);
        reject(error);
      }
    });
  },
  refreshAftersale(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        window.$api
          .newAfterSaleTypesEnum()
          .then((res) => {
            const map = {};
            (res || []).forEach(({ label, value }) => {
              map[value] = label;
            });
            commit("modify", {
              aftersaleMap: map,
            });
            resolve(res);
          })
          .catch(reject);
      } catch (error) {
        console.log("error", error);
        reject(error);
      }
    });
  },
  refreshRole(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        window.$api
          .getRole({ pageSize: 10000 })
          .then((res) => {
            const map = {};
            (res.data || []).forEach(({ id, name }) => {
              map[id] = name;
            });
            commit("modify", {
              roleMap: map,
            });
            resolve(res);
          })
          .catch(reject);
      } catch (error) {
        console.log("error", error);
        reject(error);
      }
    });
  },
  refreshDirectory(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        window.$api
          .getDirectory({
            pageSize: 100000,
            pageNumber:1
          })
          .then((res) => {
            const map = {};
            (res.data || []).forEach(({ id, name }) => {
              map[id] = name;
            });
            commit("modify", {
              directoryMap: map,
            });
            resolve(res);
          })
          .catch(reject);
      } catch (error) {
        console.log("error", error);
        reject(error);
      }
    });
  },
  refreshAccount(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        window.$api
          .getAccount({
            pageSize: 100000,
            pageNumber:1
          })
          .then((res) => {
            const map = {};
            const arr = res.data || [];
            arr.forEach(({ id, username, roleId, roleName }) => {
              map[id] = `${username} (${roleName})`;
            });
            commit("modify", {
              accountMap: map,
            });
            resolve(res);
          })
          .catch(reject);
      } catch (error) {
        console.log("error", error);
        reject(error);
      }
    });
  },
  refreshLeader(context) {
    const { commit, state } = context;
    return new Promise((resolve, reject) => {
      try {
        window.$api
          .getLeaders({
            type: 3,
          })
          .then((res) => {
            const map = {};
            const arr = res || [];
            arr.forEach(({ id, username, roleId, roleName }) => {
              map[id] = `${username} (${roleName})`;
            });
            commit("modify", {
              leaderMap: map,
            });
            resolve(res);
          })
          .catch(reject);
      } catch (error) {
        console.log("error", error);
        reject(error);
      }
    });
  },
};

const store = new Vuex.Store({
  state,
  getters,
  mutations,
  actions,
});

export default store;
